package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.vo.UserTravelNumVO;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.common.query.UserInfoQueryObject;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("mine")
public class HomePageController {
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private ITravelService travelService;

    //我的窝
    //@RequiredLogin
    @RequestMapping("/home")
    public  String homePahe(@ModelAttribute("qo")UserInfoQueryObject qo,@LoginUser UserInfo crruser,Model model){
        UserInfo userInfo = userInfoService.getById(qo.getUid());
        Integer attentionNum=userInfoService.getAttentionNumById(qo.getUid());
        Integer fansNum=userInfoService.getFansNumById(qo.getUid());
        //查询所关注的用户
        List<UserInfo>attentionUsers=userInfoService.getAttentionUser(qo.getUid());
        List<Travel> travels=travelService.getTravelsById(qo.getUid());
        Integer totalTravels = travelService.getTraveltotalByUid(qo.getUid());

        model.addAttribute("user", userInfo);
        model.addAttribute("attentionNum", attentionNum);
        model.addAttribute("fansNum", fansNum);
        model.addAttribute("attentionUsers", attentionUsers);
        model.addAttribute("travels", travels);
        model.addAttribute("totalTravels", totalTravels);
        return "homepage/homepage";
    }

    /**
     *
     * @param qo
     * @param crruser 当前用户
     * @param model
     * @return*/


    @RequiredLogin
    @RequestMapping("/mytravelnotes")
    public  String getAttention(@ModelAttribute("qo")TravelQueryObject qo, @LoginUser UserInfo crruser, Model model){

        //1查询用户数据
        UserInfo userInfo = userInfoService.getById(qo.getUid());
        model.addAttribute("user", userInfo);
        //2查询游记
        PageInfo<Travel> pageInfo = travelService.queryforUid(qo);
        model.addAttribute("pageInfo", pageInfo);
        //3查询游记数量,回复数,被阅读数
        UserTravelNumVO vo = new UserTravelNumVO();
        //查询用户游记总数存进VO
        vo.setTraveltotal(travelService.getTraveltotalByUid(qo.getUid()));
        //查询用户回复数存进VO
        vo.setReplynumtotal(travelService.getReplynumtotalByUid(qo.getUid()));
        //查询用户总被阅读数存进VO
        vo.setViewnumtotal(travelService.getViewnumtotalByUid(qo.getUid()));
        model.addAttribute("usertravel",vo);
        //4查询访客数量
        //判断进来的是自己还是其他人
        if (crruser != null) {
            if (crruser.getId() == qo.getUid()) {
                model.addAttribute("ifme", "yes");
            } else {
                userInfoService.addVisitor(qo.getUid(), crruser.getId());
            }
        }
        //最近访客
        List<UserInfo> listvisitor  = userInfoService.listvisitor(qo.getUid());
        model.addAttribute("listvisitor",listvisitor);
        //访问总数
        model.addAttribute("total", userInfoService.gettotal(qo.getUid()));
        //今日访问数
        model.addAttribute("nowdayTotal", userInfoService.nowdayTotal(qo.getUid()));

        return  "homepage/mytravelnotes";
    }

    @RequestMapping("/review")
    public  String review(){

         return "homepage/review";
    }

    @RequestMapping("/travelcollection")
    public  String travelcollection(){
        return  "homepage/travelcollection";
    }

    @RequestMapping("setting")
    public  String setting(){
        return "homepage/setting";
    }
}
